import StringBuffer from '../utils/StringBuffer.js';

/**
 * 把参数 处理成  key=value&key=value...
 */
export default class ParamsUtils {
    getParamsSign(paramMap, timeMiln) {

        //转成数组
        let paramArray = Array.from(paramMap)

        //数组升序排序
        paramArray.sort(function (a, b) {
            return a[0].localeCompare(b[0])
        })

        //转成map键值对
        let sortMap = new Map(paramArray.map(i => [i[0], i[1]]))
        //用&拼接字符串
        let mapStr = new StringBuffer()
        for (let [k, v] of sortMap) {
            mapStr.append(k + "=" + v)
            mapStr.append("&")
        }

        //map转成字符串并截取
        mapStr = mapStr.toString()
        mapStr = mapStr.substr(0, mapStr.length - 1)

        //拼接时间戳和vote
        mapStr = mapStr + timeMiln + "vote"
        // console.log("mapStr = " + mapStr)
        return mapStr
    }
}

